<script>
export default {
  name: "HeaderController",
  data(){
    return {
      user : JSON.parse(sessionStorage.getItem('CurUser'))
    }
  },

  props:{
    icon:String
  },
  methods:{
    toUser(){
      console.log('to_user')
      this.$router.push('/HomePage')
    },
    logout(){
      console.log('logout')
      this.$confirm('您确定要退出登录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '退出登录成功!'
        });
        this.$axios
            .get("/user/logout")
            .then(()=>{
              this.$store.dispatch('/logout');
              sessionStorage.clear(); // 确保清除本地存储
              this.$router.push("/");
            })
            .catch((error) => {
              this.$message.error("退出失败: " + error.message);
            });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '操作已取消'
        });
      });
    },
    collapse(){
      this.$emit('DoCollapse')
    }
  },
  created() {
    this.$router.push("/HomePage")
  }

}
</script>

<template>
  <div style="display: flex;line-height: 60px;">
    <div style="margin-top: 8px;">
      <i :class="icon" style="font-size: 20px; cursor: pointer" @click="collapse"></i>
    </div>
    <div style="flex: 1;text-align:center;font-size: 34px;">
      <span>流浪动物救助系统</span>
    </div>
    <el-dropdown>
      <span>{{user.name}}</span><i class="el-icon-user" style="margin-left: 5px"></i>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item @click.native="toUser">个人中心</el-dropdown-item>
        <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
  </div>

</template>

<style scoped>

</style>